package com.ruoyi.system.service.impl;

import java.io.IOException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.system.domain.TaskAttachment;
import com.ruoyi.system.mapper.TaskAttachmentMapper;
import com.ruoyi.system.service.ITaskAttachmentService;

/**
 * 任务附件Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class TaskAttachmentServiceImpl implements ITaskAttachmentService 
{
    @Autowired
    private TaskAttachmentMapper taskAttachmentMapper;

    /**
     * 查询任务附件列表
     * 
     * @param taskAttachment 任务附件
     * @return 任务附件
     */
    @Override
    public List<TaskAttachment> selectTaskAttachmentList(TaskAttachment taskAttachment)
    {
        return taskAttachmentMapper.selectTaskAttachmentList(taskAttachment);
    }

    /**
     * 查询任务的附件列表
     * 
     * @param taskId 任务ID
     * @param taskType 任务类型(0=主任务, 1=子任务)
     * @return 任务附件集合
     */
    @Override
    public List<TaskAttachment> selectTaskAttachmentsByTaskId(Long taskId, Integer taskType)
    {
        return taskAttachmentMapper.selectTaskAttachmentsByTaskId(taskId, taskType);
    }

    /**
     * 查询任务附件详情
     * 
     * @param attachmentId 任务附件ID
     * @return 任务附件
     */
    @Override
    public TaskAttachment selectTaskAttachmentById(Long attachmentId)
    {
        return taskAttachmentMapper.selectTaskAttachmentById(attachmentId);
    }

    /**
     * 新增任务附件
     * 
     * @param taskAttachment 任务附件
     * @return 结果
     */
    @Override
    public int insertTaskAttachment(TaskAttachment taskAttachment)
    {
        taskAttachment.setCreateTime(DateUtils.getNowDate());
        return taskAttachmentMapper.insertTaskAttachment(taskAttachment);
    }

    /**
     * 修改任务附件
     * 
     * @param taskAttachment 任务附件
     * @return 结果
     */
    @Override
    public int updateTaskAttachment(TaskAttachment taskAttachment)
    {
        taskAttachment.setUpdateTime(DateUtils.getNowDate());
        return taskAttachmentMapper.updateTaskAttachment(taskAttachment);
    }

    /**
     * 删除任务附件信息
     * 
     * @param attachmentId 任务附件ID
     * @return 结果
     */
    @Override
    public int deleteTaskAttachmentById(Long attachmentId)
    {
        return taskAttachmentMapper.deleteTaskAttachmentById(attachmentId);
    }

    /**
     * 批量删除任务附件
     * 
     * @param attachmentIds 需要删除的任务附件ID
     * @return 结果
     */
    @Override
    public int deleteTaskAttachmentByIds(Long[] attachmentIds)
    {
        return taskAttachmentMapper.deleteTaskAttachmentByIds(attachmentIds);
    }

    /**
     * 上传任务附件
     * 
     * @param taskId 任务ID
     * @param taskType 任务类型(0=主任务, 1=子任务)
     * @param file 文件
     * @return 任务附件
     */
    @Override
    public TaskAttachment uploadTaskAttachment(Long taskId, Integer taskType, MultipartFile file) throws Exception
    {
        // 上传文件到指定目录
        String filePath = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);

        TaskAttachment taskAttachment = new TaskAttachment();
        taskAttachment.setTaskId(taskId);
        taskAttachment.setTaskType(taskType);
        taskAttachment.setFileName(file.getOriginalFilename());
        taskAttachment.setFilePath(filePath);
        taskAttachment.setFileSize(file.getSize());

        String originalFilename = file.getOriginalFilename();
        String fileType = StringUtils.substringAfterLast(originalFilename, ".");
        taskAttachment.setFileType(fileType);

        insertTaskAttachment(taskAttachment);
        
        return taskAttachment;
    }
} 